/**
 * 
 */
function ProgressDialog(prefix, complex) {
	
	var UNKNOWN = -1;
	var initialized = false;
	var progressId;
	var me = this;
	var initState = { done: -1, total: -1, donePercent: -1, message: 'Initializing...' };
	
	var $progressDlg;
	var progressItem = { $bar: null, $box: null };
	var subProgressItem = { $bar: null, $box: null };
	
	this.init = function() {
		if (!initialized) {
			initialized = true;
			createProgressDialog();
			updateProgressItem(progressItem, initState);
			if (complex) {
				updateProgressItem(subProgressItem, initState);	
			}
			$.get('/book-app/progress/init', null, progressResponseHandler, 'json');
		}
	};
	
	
	function createProgressDialog() {
		/*var progressDlgTmpl = $.template(null, '<div id="${prefix}ProgressDialog"></div>');
		var progressBoxTmpl = $.template(null, '<span id="${prefix}ProgressText"></span>'
												+ '<div id="${prefix}ProgresBar"></div>');*/
		$.template('progressDlgTmpl', '<div id="${prefix}ProgressDialog"></div>');
		$.template('progressBoxTmpl', '<div><span id="${prefix}ProgressText"></span>'
										+ '<div id="${prefix}ProgresBar"></div></div>');
		var progressDlgProps = { modal: true, resizable: false, minWidth: '400' };
		var progressDlgCss = { verticalAlign: 'middle', textAlign: 'left' };
		
		$progressDlg = $.tmpl('progressDlgTmpl', { prefix: prefix }).dialog(progressDlgProps).css(progressDlgCss);
		$('<h3></h3>').hide().appendTo($progressDlg);
		progressItem.$box = $.tmpl('progressBoxTmpl', { prefix: (prefix + 'Main') }).appendTo($progressDlg);
		progressItem.$bar = progressItem.$box.find('div').progressbar();
		if (complex) {
			subProgressItem.$box = $.tmpl('progressBoxTmpl', prefix + 'Sub').appendTo($progressDlg);
			subProgressItem.$bar = subProgressItem.$box.find('div').progressbar();
		}
		
		$progressDlg.parent().find('.ui-dialog-titlebar').css({ display: 'none' });
	}
	
	this.ask = function() {
		$.get('/book-app/progress/' + progressId, null, progressResponseHandler, 'json');
	};
	
	this.delayedAsk = function() {
		setTimeout(this.ask, 250);
	};
	
	function progressResponseHandler(response) {
		if (response.type == 'INIT') {
			progressId = response.progressId;
			if (response.label) {
				$progressDlg.find('h3:first').html(response.label);
				$progressDlg.find('h3').show();
			}
			me.ask();
		} else if (response.type == 'MORE' || response.type == 'DONE') {
			updateProgressItem(progressItem, response.state);
			if (complex) {
				updateProgressItem(subProgressItem, response.subState);
			}
			if (response.type == 'MORE') {
				me.delayedAsk();
			} else if (response.type == 'DONE') {
				$progressDlg.dialog({ buttons: [{ 
					text: 'Ok', 
					click: function() { $(this).dialog('close'); } 
				}] });
			}
		} else if (response.type == 'ERROR') {
			
		}
	}
	
	function updateProgressItem(item, state) {
		item.$bar.progressbar({ value: state.donePercent });
		item.$box.find('span:first').html(toStateString(state));
	}
	
	function toStateString(state) {
		return '[' + toString(state.done) + '/' + toString(state.total) 
				+ ' | ' + toString(state.donePercent) + '%] ' + state.message;		
	}
	
	function toString(value) {
		return value == UNKNOWN ? '?' : value;
	}
}